<?php

namespace App\Console\Commands;

use App\Imports\MultipleImport;
use App\Models\JobType;
use App\Models\ModelNumber;
use App\Models\ModelNumberSpecification;
use App\Models\Product;
use App\Models\ProductCenter;
use App\Models\Specification;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class AddJobTypePrice extends Command
{
    /**
     * 导入工种价格
     * The name and signature of the console command.
     * php artisan command:addjobtypeprice
     * @var string
     */
    protected $signature = 'command:addjobtypeprice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //$file = public_path('excel').'/工序工价.xlsx';//获取文件路径
        $file = public_path('excel').'/副本产品工价(1).xlsx';//获取文件路径
        $import = new MultipleImport(1);
        Excel::import($import, $file);
        $results = $import->sheet[0]->data->toArray();
        //$results2 = $import->sheet[1]->data->toArray();
        //$results3 = $import->sheet[2]->data->toArray();
        //dd($results,$results2,$results3);
        unset($results[0]);
        unset($results[1]);
        unset($results[2]);
        $results_s = $results;
        foreach($results as $k=>$v){

//            if(empty($v[1])){
//                //p($results_s[$k-1][1]);
//                $results_s[$k][1] =  $results_s[$k-1][1];
//            }
            if(empty($v[2])){
                $results_s[$k][2] =  $results_s[$k-1][2];
            }
            if(empty($v[3])){
                $results_s[$k][3] =  $results_s[$k-1][3];
            }
        }
//        foreach($results_s as $k=>$v){
//            $this->info('导入：产品名称='.$v[1].";型号=".$v[2].";规格=".$v[3].";工种=".$v[4].";工种价格=".$v[5]);
//            if(empty($v[5])){
//                continue;
//            }
//            if(empty($v[4])){
//                continue;
//            }
//            if(empty($v[3])){
//                continue;
//            }
//            if(empty($v[2])){
//                continue;
//            }
//            if(empty($v[1])){
//                continue;
//            }
//            $product = Product::where('product_name',$v[1])->first();
//            $model_number = ModelNumber::updateOrCreate(['model_number'=>$v[2]],['model_number'=>$v[2]]);
////            if($v[1] == '沙发'){
////                if($v[4] == '包工组'){
////                    $v[4] = '沙发包工组';
////                }
////            }elseif ($v[1] == '软床'){
////                if($v[4] == '包工组'){
////                    $v[4] = '软床包工组';
////                }
////            }
////            if($v[4] == '质检'){
////                $v[4] = '厂长+质检';
////            }
//            $job_type = JobType::updateOrCreate(['job_type'=>$v[4]],['job_type'=>$v[4]]);
//            $arr = [];
//            if(strpos($v[3],"\n")){
//                $arr = explode("\n",$v[3]);
//                //p($arr);
//                foreach($arr as $kk=>$vv){
//                    if(empty($vv)){
//                        continue;
//                    }
//                    $specification = Specification::updateOrCreate(['specification'=>$vv],['specification'=>$vv]);
//                    ModelNumberSpecification::updateOrCreate(
//                        [
//                            'model_number_id'=>$model_number->id,
//                            'specification_id'=>$specification->id,
//                            'product_id'=>$product->id
//                        ],
//                        [
//                            'model_number_id'=>$model_number->id,
//                            'specification_id'=>$specification->id,
//                            'product_id'=>$product->id
//                        ]
//                    );
//                    $where = [
//                        'product_id'=>$product->id,
//                        'model_number_id'=>$model_number->id,
//                        'specification_id'=>$specification->id,
//                        'job_type_id'=>$job_type->id
//                    ];
//                    ProductCenter::updateOrCreate($where,[$where,[$where,'price'=>$v[5]]]);
//                }
//            }else{
//                $specification = Specification::updateOrCreate(['specification'=>$v[3]],['specification'=>$v[3]]);
//                ModelNumberSpecification::updateOrCreate(
//                    [
//                        'model_number_id'=>$model_number->id,
//                        'specification_id'=>$specification->id,
//                        'product_id'=>$product->id
//                    ],
//                    [
//                        'model_number_id'=>$model_number->id,
//                        'specification_id'=>$specification->id,
//                        'product_id'=>$product->id
//                    ]
//                );
//                $where = [
//                    'product_id'=>$product->id,
//                    'model_number_id'=>$model_number->id,
//                    'specification_id'=>$specification->id,
//                    'job_type_id'=>$job_type->id
//                ];
//                ProductCenter::updateOrCreate($where,[$where,'price'=>$v[5]]);
//            }
//
//
//
//        }
        //p($results_s);
        foreach($results_s as $k=>$v){
            //p($v);
            if($v[5] == ''){
                continue;
            }
            $product = Product::where('product_name',$v[1])->first();
            $model_number = ModelNumber::where('model_number',$v[2])->first();
            $specification = Specification::where('specification',$v[3])->first();
            $job_type = JobType::where('job_type',$v[4])->first();
            $where = [
                'product_id'=>$product->id,
                'model_number_id'=>$model_number->id,
                'specification_id'=>$specification->id,
                'job_type_id'=>$job_type->id
            ];
            ProductCenter::updateOrCreate($where,[$where,'price'=>$v[5]]);
        }

    }


}
